const app = getApp()
var setInteshow = null;
import { activeCard, addCard } from '../../../services/card';
import { getCardInfo, getUserInfo, } from '../../../services/user';

Page({
  data: {
    animationData: {},//动画
    cameraOpen: false,
    result: '',//扫码结果
    userInfo:wx.getStorageSync('userInfo')
  },
  onLoad: function () {
    console.log(this.data.userInfo)
    // let that = this;
    // that.setData({
    //   cameraOpen: true
    // });
    // clearInterval(setInteshow);
    // var i = 0;
    // var animation = wx.createAnimation({
    //   duration: 2600,
    //   timingFunction: 'ease-in-out',
    // })
    // animation.translateY(0).step({ duration: 40 }).translateY(260).step({ duration: 2600 });
    // this.setData({
    //   animationData: animation.export()
    // })
    // //连续动画 开发者工具会执行,真机调试只执行一次
    // setInteshow = setInterval(function () {
    //   that.setData({
    //     animationData: {}
    //   })
    //   animation.translateY(-260).step({ duration: 40 });
    //   animation.translateY(260).step({ duration: 2600 });
    //   this.setData({
    //     animationData: animation.export()
    //   })
    //   console.log('动画执行次?', i += 1);
    // }.bind(this), 2600);
  },
  //打开相机
  bindOpen() {

  },
  //用户不允许使用摄像头时触发
  errorFun() {
    console.log('用户不允许使用摄像头时触发');
    let that = this;
    that.setData({
      is_scan: false,
      cameraOpen: false
    });
    //调用然后微信自己带的扫码器
    // wx.scanCode({
    //   onlyFromCamera: true,
    //   success(res) {
    //     console.log('扫码得到的结果', res.result)
    //     let arr = res.result.split('=');
    //     that.is_canBack(arr[1]);
    //   }
    // })
  },
  //摄像头在非正常终止时触发，如退出后台等情况
  bindstop() {
    let that = this;
    that.setData({
      is_scan: false
    })
    console.log('摄像头在非正常终止时触发，如退出后台等情况');
  },
  //相机是否加载成功
  loadSuccess(e) {
    console.log('相机是否加载成功', e);
    let that = this;
    that.setData({
      is_scan: true
    })
  },
  //扫码成触发的事件
  scanSuccess(e) {
    if (this.data.userInfo) {
      wx.navigateTo({
        url: '/subPackageWork/pages/login/index',
      });
    } else {
      let self = this;
      if (e.detail.result.slice(0, 2) === '00') {
        wx.setStorageSync('card_no', e.detail.result);
        wx.navigateTo({
          url: `/subPackageWork/pages/exchange/index?type=coupon`,
        });
      } else {
        getCardInfo({
          card_no: e.detail.result,
        }).then(data => {
          if (data.status === 6) {
            wx.setStorageSync('card_no', data.card_no);
            wx.navigateTo({
              url: `/subPackageWork/pages/exchange/index`,
            });
          } else if (data.status === 4) {
            addCard({
              card_no: data.card_no,
            }).then(
              res => {
                wx.showToast({
                  title: '添加成功',
                  icon: 'none',
                });
                // 二级代理从一级代理领卡
                let contentArr = [
                  {
                    key: '卡号',
                    value: data.card_no,
                  },
                  {
                    key: '类型',
                    value: data.attr_name,
                  },
                ];
                self.setState({
                  contentArr: contentArr,
                  isShowModal: true,
                  cardInfo: data,
                  modalTitle: '已加入我的兑换卡',
                  comfirmText: '继续添加',
                  cancleText: '关闭',
                });
              },
              () => {
                this.setState({
                  isShowModal: false,
                });
              }
            );
          } else if (data.status === 5) {
            let contentArr = [
              {
                key: '卡号',
                value: data.card_no,
              },
              {
                key: '类型',
                value: data.attr_name,
              },
            ];
            self.setState({
              contentArr: contentArr,
              isShowModal: true,
              cardInfo: data,
              modalTitle: '兑换卡',
              comfirmText: '去付款',
              cancleText: '取消',
            });
          } else {
            // Taro.showToast({
            //   title: '卡的状态不对',
            //   icon: 'none'
            // })
          }
        });
      }
    }
    wx.showToast({
      title: '扫码成功',
    })
  },
  //关闭扫码
  closeCamera() {
    console.log('用户主动关闭');
    let that = this;
    clearInterval(setInteshow);
    that.setData({
      cameraOpen: false
    })
  }
})
